﻿using System;
using System.Collections.Generic;
using shfa.snomtastic.admin.components;
using shfa.snomtastic.data.business;
using Telerik.Web.UI;
using System.Web.UI;
using shfa.snomtastic.data;

namespace shfa.snomtastic.admin.editcontrols
{
    public partial class eSubnets : EditBase
    {

        #region Page

        protected void Page_Load(object sender, EventArgs e)
        {
            BindGridItems();
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            rgListItems.ItemCommand += new GridCommandEventHandler(rgListItems_ItemCommand);
            ScriptManager.RegisterClientScriptBlock(this, typeof(EditBase), "ClientEvents", common.DblClickScript, true);
        }

        #endregion

        #region Event Handlers


        protected void rgListItems_ItemCommand(object source, GridCommandEventArgs e)
        {

            if (e.CommandName == "Edit")
            {
                e.Canceled = true;
                Guid id = new Guid(rgListItems.Items[e.Item.ItemIndex].GetDataKeyValue("SubnetId").ToString());
                RaiseNavEvent(this, new NavEventArgs("eSubnet", id));

            }
        }

        #endregion


        public void BindGridItems()
        {
            SubnetController sc = new SubnetController();
            List<SubnetInfo> siList = sc.SelectSubnets();

            ConfigurationController cc = new ConfigurationController();

            foreach (SubnetInfo si in siList)
            {
                if (si.ConfigurationId != Guid.Empty)
                {
                    ConfigurationInfo ci = cc.SelectConfiguration(si.ConfigurationId);
                    if (ci!=null)
                    {
                        si.Configuration = ci.Name;
                    }
                }
            }

            rgListItems.DataSource = siList;
            rgListItems.DataBind();


        }


        #region Action event Helpers

        private void DeleteItem(Guid id)
        {
            SubnetController sc = new SubnetController();
            sc.DeleteSubnet(id);
            RaiseNavEvent(this, new NavEventArgs("eSubnets", new Guid()));
        }

        #endregion



        #region EditBase

        public override void EditBase_ActionEvent(object sender, ActionClickArgs e)
        {
            switch (e.ActionName)
            {
                case "Add":
                    RaiseNavEvent(this, new NavEventArgs("eSubnet", new Guid()));
                    break;
                case "Edit":
                    if (rgListItems.SelectedValue != null)
                    {
                        RaiseNavEvent(this, new NavEventArgs("eSubnet", new Guid(rgListItems.SelectedValue.ToString())));
                    }
                    break;
                case "Delete":
                    if (rgListItems.SelectedValue != null)
                    {
                        DeleteItem(new Guid(rgListItems.SelectedValue.ToString()));
                    }
                    break;
                default:
                    break;
            }
        }

        public override List<ActionInfo> ActionList
        {
            get
            {
                List<ActionInfo> al = new List<ActionInfo>();
                al.Add(new ActionInfo("Add", "Add Subnet", "~/images/icons/add.png"));
                al.Add(new ActionInfo("Edit", "Edit Subnet", "~/images/icons/edit.png"));
                al.Add(new ActionInfo("Delete", "Delete Subnet", "~/images/icons/delete.png", "Are you sure you want to delete this Subnet?", true));
                return al;
            }
        }

        #endregion

    }
}